package com.example;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.jdbc.core.JdbcTemplate;

import com.example.exceptions.WrongDirectionSpecificationException;
import com.example.models.Customer;
import com.example.models.Product;
import com.example.models.UserRole;
import com.example.models.Users;
import com.example.repositories.CartRepository;
import com.example.repositories.CustomerRepository;
import com.example.repositories.ItemToPurchaseRepository;
import com.example.repositories.ProductRepository;
import com.example.repositories.UserRoleRepository;
import com.example.repositories.UsersRepository;
import com.mongodb.MongoClient;

@SpringBootApplication
public class ShoppingCartApiApplication {

	private static final Logger log = LoggerFactory.getLogger(ShoppingCartApiApplication.class);
	
	public static void main(String[] args) {
		SpringApplication.run(ShoppingCartApiApplication.class, args);
	}
	
	@Autowired
	JdbcTemplate jdbcTemplate;
	
//	@Bean
	CommandLineRunner init(ProductRepository productRepository,
			CartRepository cartRepository, ItemToPurchaseRepository itemToPurchaseRepository,
			UsersRepository usersRepository, UserRoleRepository userRoleRepository,
			CustomerRepository customerRepository) {
		return (args) ->{
			
			/** DROP TABLES MONGODB **/
			MongoClient mongo = new MongoClient("localhost", 27017);
			MongoOperations mongoOps = new MongoTemplate(mongo, "shopping_cart");
			mongoOps.dropCollection("users");
			mongoOps.dropCollection("userRole");
			mongoOps.dropCollection("product");
			mongoOps.dropCollection("customer");
			mongoOps.dropCollection("cart");			
			mongo.close();
//			MongoDB create tables authomathically, while in MySQL you need a script with sql code
			
			/** DROP TABLES MySQL **/
//			jdbcTemplate.execute("DROP TABLE IF EXISTS cart_items");
//			jdbcTemplate.execute("DROP TABLE IF EXISTS item_to_purchase");
//			jdbcTemplate.execute("DROP TABLE IF EXISTS cart");
//			jdbcTemplate.execute("DROP TABLE IF EXISTS product");
//			jdbcTemplate.execute("DROP TABLE IF EXISTS customer");
//			jdbcTemplate.execute("DROP TABLE IF EXISTS user_role");
//			jdbcTemplate.execute("DROP TABLE IF EXISTS users");
			
			
			Users user = new Users("ilancho", "ilancho", true);
			usersRepository.save(user);
			Users user2 = new Users("juancho", "juancho", true);
			usersRepository.save(user2);
			
			UserRole rol = new UserRole("ilancho", "ROLE_USER");
			userRoleRepository.save(rol);
			rol = new UserRole("ilancho", "ROLE_ADMIN");
			userRoleRepository.save(rol);
			rol = new UserRole("juancho", "ROLE_USER");
			userRoleRepository.save(rol);
			
			Customer customer1 = new Customer("ilan", "rosenfeld", "ilancho");
			Customer customer2 = new Customer ("juan", "perez", "juancho" );
			customerRepository.save(customer1);
			customerRepository.save(customer2);
			
			SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
			Product p1 = new Product("comida", "torta",15,100, formatter.parse("10-01-2020"));
			Product p2 = new Product("bebida", "cerveza",25,800, formatter.parse("10-01-2021"));
			Product p3 = new Product("comida", "manzana",5,200, formatter.parse("10-01-2022"));
			Product p4 = new Product("bebida", "coca",35,700, formatter.parse("10-01-2023"));
			Product p5 = new Product("comida", "pie",15,300, formatter.parse("10-01-2024"));
			Product p6 = new Product("bebida", "sprite",20,600, formatter.parse("10-01-2025"));
			Product p7 = new Product("verdura", "tomate",15,400, formatter.parse("10-01-2026"));
			Product p8 = new Product("bebida", "fanta",18,500, formatter.parse("10-01-2027"));
			
			productRepository.save(p1);
			productRepository.save(p2);
			productRepository.save(p3);
			productRepository.save(p4);
			productRepository.save(p5);
			productRepository.save(p6);
			productRepository.save(p7);
			productRepository.save(p8);
			
			// fetch all products
			List<Product> products= productRepository.findAll();
			log.info("Products found:");
			log.info("--------------------------------");
			for(Product prod: products){
					log.info(prod.toString());
			}
			log.info("");
			
            /** END CREATE TABLES AND ROWS **/
		};
	}

}
